﻿using System.Collections.Generic;
using System.Data;
using RpcHelper;
using RpcModel;

using SqlExecHelper;

using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class RemoteServerTypeDAL : SqlBasicClass
        {
                public RemoteServerTypeDAL () : base ("RemoteServerType")
                {

                }

                public long AddServiceType (ServerTypeDatum add)
                {
                        base.Insert (add, out long id);
                        return id;
                }
                public bool CheckIsRepeat (string typeVal)
                {
                        return base.CheckIsExists (new SqlWhere ("TypeVal", SqlDbType.VarChar, 50) { Value = typeVal });
                }

                public bool CheckIsExists (long groupId)
                {
                        return base.CheckIsExists (new SqlWhere ("GroupId", SqlDbType.BigInt) { Value = groupId });
                }

                public void Set (long id, ServerTypeSetParam param)
                {
                        if (!base.Update (param, "Id", id))
                        {
                                throw new ErrorException ("rpc.server.type.set.error");
                        }
                }

                public void Delete (long id)
                {
                        if (!base.Drop ("Id", id))
                        {
                                throw new ErrorException ("rpc.server.type.drop.error");
                        }
                }

                public ServerType Get (long id)
                {
                        return base.GetRow<long, ServerType> ("Id", id);
                }
                public ServerType[] GetServiceTypes (long groupId)
                {
                        return base.Get<long, ServerType> ("GroupId", groupId);
                }
                public ServerType[] Query (ServerTypeQueryParam query, IBasicPage paging, out long count)
                {
                        List<ISqlWhere> param = new List<ISqlWhere> (3);
                        if (query.GroupId != 0)
                        {
                                param.Add (new SqlWhere ("GroupId", SqlDbType.BigInt) { Value = query.GroupId });
                        }
                        if (!string.IsNullOrEmpty (query.Name))
                        {
                                param.Add (new LikeSqlWhere ("SystemName", 50, LikeQueryType.全) { Value = query.Name });
                        }
                        if (query.BalancedType.HasValue)
                        {
                                param.Add (new SqlWhere ("BalancedType", SqlDbType.SmallInt) { Value = query.BalancedType });
                        }
                        return base.Query<ServerType> ("Id desc", paging.Index, paging.Size, out count, param.ToArray ());
                }

                internal BasicServerType[] GetBasic (long[] ids)
                {
                        return base.Get<long, BasicServerType> ("id", ids);
                }

                internal ServerType[] Gets (long[] ids)
                {
                        return base.Get<long, ServerType> ("id", ids);
                }
                internal ServerType[] Gets(string[] types)
                {
                        return base.Get<string, ServerType>("TypeVal", types);
                }
                internal BasicServerType[] GetBasic(string[] types)
                {
                        return base.Get<string, BasicServerType>("TypeVal", types);
                }
                internal void Clear (long groupId)
                {
                        if (!this.Drop ("GroupId", groupId))
                        {
                                throw new ErrorException ("rpc.server.type.clear.error");
                        }
                }
        }
}
